package com.music.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.dao.AlbumMapper;
import com.music.dao.SingerMapper;
import com.music.entity.Album;
import com.music.entity.Seckill;
import com.music.service.AlbumService;
import com.music.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 肖泽强
 */
@Service
@Slf4j
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SingerMapper singerMapper;


    /**
     *
     * @param flag true 降序  false升序
     * @param size  获取条数的大小
     * @return 专辑集合
     */
    //获取新上架的五款专辑
    public List<Album> getNewPutawayForFive(boolean flag,Integer size){
        Map<String,Object> map = new HashMap<>();
        map.put("rank",flag);
        PageHelper.startPage(1,size);
        List<Album> albumsList = this.albumMapper.findAlbumByConditions(map);
        //设置歌手信息
       // setSinger(albumsList);
        return albumsList;
    }
    /**
     *
     * @param flag true 降序  false升序
     * @param size  获取条数的大小
     * @param districtId  地区所属
     * @return 专辑集合
     */
    //获取最火的五款专辑和 地区所属(可选)
    public List<Album> getHotAlbum(boolean flag,Integer size,Integer districtId){
        Map<String,Object> map = new HashMap<>();
        if(districtId > 0){
            map.put("districtId",districtId);
        }
        map.put("hot",flag);
        PageHelper.startPage(1,size);
        List<Album> albumsList = this.albumMapper.findAlbumByConditions(map);

        //设置歌手信息
//        albumsList.forEach(album -> {
//            album.setSinger(singerMapper.selectByPrimaryKey(album.getSingerId()));
//        });
//        for (int i = 0; i < albumsList.size(); i++) {
//            albumsList.get(i).setSinger(singerMapper.selectByPrimaryKey(albumsList.get(i).getSingerId()));
//        }
        return albumsList;
    }

//    public void setSinger(List<Album> albumList){
//        albumList.forEach(album -> {
//            //查询歌手
//            album.setSinger(singerMapper.selectByPrimaryKey(album.getSingerId()));
//
//            //查询歌曲
//
//            //查询地区
//
//            //查询流派
//        });
//    }
    //获取秒杀专辑接口
    public static void show(SeckillService seckillService){
        seckillService.getAlbumSeckillGoodsByConditions();
    }

    //根据条件获取专辑
    @Override
    public List<Album> getAlbumByConditions(Map<String, Object> map) {

        List<Album> albumByConditions = albumMapper.findAlbumByConditions(map);
        return albumByConditions;
    }

    @Override
    public List<Seckill> getAlbumSeckillAll(String id) {
            ArrayList<Seckill> seckillList = new ArrayList<>();
            if(id != null && !"".equals(id.trim())){
                Seckill seckill = (Seckill) redisTemplate.opsForHash().get("seckillGoods", id);
                if(seckill == null){
                    return null;
                }
                seckillList.add(seckill);
                seckill.setQuantityOfCommodity(seckill.getQuantityOfCommodity()-1);
                if(seckill.getQuantityOfCommodity() <= 0){
                    redisTemplate.opsForHash().delete("seckillGoods",seckill.getSeckillId());
                    redisTemplate.opsForList().remove("seckillGoodsList",1,seckill.getSeckillId());
                }else{
                    redisTemplate.opsForHash().put("seckillGoods",seckill.getSeckillId(),seckill);
                }
            }else {
                show(() -> {
                    Map seckillGood = redisTemplate.opsForHash().entries("seckillGoods");
                    List goodsList = redisTemplate.opsForList().range("seckillGoodsList", 0, -1);
                    seckillGood.forEach((key, value) -> {
                        goodsList.forEach(seckillId -> {
                            if (seckillId.equals(key)) {
                                seckillList.add((Seckill) value);
                            }
                        });
                    });
                });
            }
        return seckillList;
    }

    @Override
    public List<Album> findAlbumList(Album album) {
        return albumMapper.findByCondition(album);
    }


}




